package com.ruoyi.web.controller.wms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsLocation;
import com.ruoyi.wms.service.IWmsLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 库位信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@Api(tags = "库位信息管理")
@RestController
@RequestMapping("/wms/location")
public class WmsLocationController extends BaseController
{
    @Autowired
    private IWmsLocationService wmsLocationService;

    /**
     * 查询库位信息列表
     */
    @ApiOperation(value = "获取库位信息列表", notes = "获取库位信息列表，支持分页和条件过滤")
    @PreAuthorize("@ss.hasPermi('wms:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsLocation wmsLocation)
    {
        startPage();
        List<WmsLocation> list = wmsLocationService.selectWmsLocationList(wmsLocation);
        return getDataTable(list);
    }

    /**
     * 导出库位信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:location:export')")
    @Log(title = "库位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsLocation wmsLocation)
    {
        List<WmsLocation> list = wmsLocationService.selectWmsLocationList(wmsLocation);
        ExcelUtil<WmsLocation> util = new ExcelUtil<WmsLocation>(WmsLocation.class);
        util.exportExcel(response, list, "库位信息数据");
    }

    /**
     * 获取库位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsLocationService.selectWmsLocationById(id));
    }

    /**
     * 新增库位信息
     */
    @PreAuthorize("@ss.hasPermi('wms:location:add')")
    @Log(title = "库位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsLocation wmsLocation)
    {
        if (!wmsLocationService.checkLocationCodeUnique(wmsLocation))
        {
            return error("新增库位'" + wmsLocation.getLocationName() + "'失败，库位编码已存在");
        }
        wmsLocation.setCreateBy(getUsername());
        return toAjax(wmsLocationService.insertWmsLocation(wmsLocation));
    }

    /**
     * 修改库位信息
     */
    @PreAuthorize("@ss.hasPermi('wms:location:edit')")
    @Log(title = "库位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsLocation wmsLocation)
    {
        if (!wmsLocationService.checkLocationCodeUnique(wmsLocation))
        {
            return error("修改库位'" + wmsLocation.getLocationName() + "'失败，库位编码已存在");
        }
        wmsLocation.setUpdateBy(getUsername());
        return toAjax(wmsLocationService.updateWmsLocation(wmsLocation));
    }

    /**
     * 检查库位编码唯一性
     */
    @PostMapping("/checkLocationCodeUnique")
    public AjaxResult checkLocationCodeUnique(@RequestBody WmsLocation wmsLocation)
    {
        boolean isUnique = wmsLocationService.checkLocationCodeUnique(wmsLocation);
        return success(isUnique);
    }

    /**
     * 删除库位信息
     */
    @PreAuthorize("@ss.hasPermi('wms:location:remove')")
    @Log(title = "库位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsLocationService.deleteWmsLocationByIds(ids));
    }

    /**
     * 根据库位编码查询库位信息
     */
    @ApiOperation(value = "根据库位编码查询库位信息", notes = "根据库位编码查询库位详细信息")
    @ApiImplicitParam(name = "locationCode", value = "库位编码", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('wms:location:query')")
    @GetMapping("/getByCode/{locationCode}")
    public AjaxResult getByCode(@PathVariable String locationCode)
    {
        WmsLocation location = wmsLocationService.selectWmsLocationByCode(locationCode);
        return success(location);
    }

    /**
     * 获取推荐库位
     */
    @ApiOperation(value = "获取推荐库位", notes = "根据商品ID和仓库ID获取推荐库位信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "itemId", value = "商品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping("/recommended/{itemId}/{warehouseId}")
    public AjaxResult getRecommendedLocation(@PathVariable Long itemId, @PathVariable Long warehouseId)
    {
        WmsLocation location = wmsLocationService.getRecommendedLocation(itemId, warehouseId);
        return success(location);
    }
}